﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm.ParticleSystem
{

    public class FlameThrown : ParticleSystem<FlameThrown.Particle>
    {
        public class Particle : Game.Particle
        {
        }

        public Game.Random m_random = new Game.Random();

        public Vector3 m_position;

        public Vector3 m_direction;

        public Color m_color = Color.White;

        public int TextureAmount = 9;

        public FlameThrown(SubsystemTerrain terrain, Vector3 position, Vector3 direction)
            : base(10)
        {
            Texture = ContentManager.Get<Texture2D>("Textures/FireParticle");
            TextureSlotsCount = 3;
            m_position = position;
            m_direction = Vector3.Normalize(direction);
            m_position += m_direction * 2f;
        }
        public override bool Simulate(float dt)
        {
            for (int i = 0; i < base.Particles.Length; i++)
            {
                Particle particle = base.Particles[i];
                particle.Position = m_position + m_direction * i * dt + new Vector3(m_random.Float(-0.1f, 0.1f), m_random.Float(-0.1f, 0.1f), m_random.Float(-0.1f, 0.1f));
                particle.TextureSlot = m_random.Int(0, 5);
                particle.Size = new Vector2(0.3f);
            }
            if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(0.7, 0.0)) return true;
            return false;
        }
        public Vector4 GetCoordsBySlot(int slot)
        {
            float ec = 1f / TextureSlotsCount;
            int y = slot / TextureSlotsCount;
            int x = slot % TextureAmount;
            return new Vector4(x * ec, y * ec, (x + 1) * ec, (y + 1) * ec);
        }
        public override void Draw(Camera camera)
        {
            Vector3 viewDirection = camera.ViewDirection;
            Vector3 vector = Vector3.Normalize(Vector3.Cross(viewDirection, Vector3.UnitY));
            Vector3 v = -Vector3.Normalize(Vector3.Cross(vector, viewDirection));

            for (int i = 0; i < base.Particles.Length; i++)
            {
                Vector3 position = Particles[i].Position;
                Vector3 p;
                Vector3 p2;
                Vector3 p3;
                Vector3 p4;
                vector *= Particles[i].Size.Y;
                v *= Particles[i].Size.X;
                p = position + (-vector - v);
                p2 = position + (vector - v);
                p3 = position + (vector + v);
                p4 = position + (-vector + v);
                Vector4 vector4 = GetCoordsBySlot(Particles[i].TextureSlot);
                TexturedBatch3D texturedBatch3D = SubsystemParticles.PrimitivesRenderer.TexturedBatch(Texture, true, 1, null, null, BlendState.NonPremultiplied);
                texturedBatch3D.QueueQuad(p, p2, p3, p4, new Vector2(vector4.X, vector4.Y), new Vector2(vector4.Z, vector4.Y), new Vector2(vector4.Z, vector4.W), new Vector2(vector4.X, vector4.W), Color.White);
            }

        }

    }
}

